<?php

/**
 * Description of ErrorHandler
 * @package Core
 * @author superx
 */
class Core_ErrorHandler {

    public static function init() {
        set_error_handler(array('Core_ErrorHandler', 'exceptionHandler'), E_ALL & ~E_DEPRECATED & ~E_STRICT);
        register_shutdown_function(array('Core_ErrorHandler', 'shutdownHandler'), PHP_SAPI);
    }

    public static function exceptionHandler($errno, $errstr, $errfile, $errline, $errcontext) {
        throw new ErrorException($errstr, $errno, 1, $errfile, $errline);
    }

    public static function shutdownHandler($sapi = '') {
        if ($error = error_get_last()) {
            if ($error['type'] == E_ERROR || ENV == 'dev') {
                if ($sapi == 'cli') {
                    $log_name = 'cliError';
                } else {
                    $log_name = 'sysError';
                }
                $log = "==============================================================>\n";
                $log .= $error['message'] . "\n" . $error['file'] . ' Line ' . $error['line'];
                $log .= "\n<===================================================================================\n\n";
                Core_Log::log($log, $log_name);
            }
        }
    }

}
